//
//  RegistViewController.m
//  InviteJob
//
//  Created by LangLang on 17/10/31.
//  Copyright © 2017年 Shui. All rights reserved.
//

#import "ForgetPasswordViewController.h"
#import "UIButton+Timer.h"
@interface ForgetPasswordViewController ()

@property (weak, nonatomic) IBOutlet UIButton *btnCode;

@property (weak, nonatomic) IBOutlet UITextField *txtMoblie;

@property (weak, nonatomic) IBOutlet UITextField *txtCode;
@property (weak, nonatomic) IBOutlet UITextField *txtPasswd;
@property (weak, nonatomic) IBOutlet UITextField *txtSurePasswd;


@end

@implementation ForgetPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    TitleView(@"忘记密码")
    [self codeUI];
    
}

-(void)codeUI{
    
    _btnCode.layer.cornerRadius = 3;
    _btnCode.layer.borderWidth = 0.5;
    _btnCode.layer.borderColor = NavBackColor.CGColor;
    _btnCode.layer.masksToBounds = YES;
    
    _txtPasswd.secureTextEntry = YES;
    _txtSurePasswd.secureTextEntry = YES;
    
}

- (IBAction)btnCodeOnClick:(id)sender {
    if ([SHJudgeTools valiMobile:_txtMoblie.text]) {
        SHuiMBhudTextTimeStr(@"请输入正确的手机号");
        return;
    }
    [self loadRegist];
    
}

// 获取验证码
-(void)loadRegist{
    
    NSString *strUrl = [NSString stringWithFormat:user_forgetPwd_code_api];
    
    NSDictionary *parameter = @{@"phone":_txtMoblie.text};
    
    [PostNetWorking postNetWorkingStr:strUrl andparameters:parameter Success:^(id responseObject) {
        
        if ([responseObject[@"code"] intValue] == 200) {
            _btnCode.time = 60;
            _btnCode.format = @"%lds";
            [_btnCode startTimer];
            SHuiMBhudTextTimeStr(@"发送成功");
        }else{
            NSString *mark = [NSString stringWithFormat:@"%@",responseObject[@"mark"]];
            SHuiMBhudTextTimeStr(mark);
        }
        
    } failed:^(NSError *error) {
        SHuiMBhudTextTimeStr(@"网络错误");
    }];
    
}

- (IBAction)btnSure:(id)sender {
    
    if ([SHJudgeTools valiMobile:_txtMoblie.text]) {
        SHuiMBhudTextTimeStr(@"请输入手机号");
        return;
    }
    if (_txtCode.text.length == 0) {
        SHuiMBhudTextTimeStr(@"请输入验证码");
        return;
    }
    if (![SHJudgeTools validatePassword:_txtPasswd.text]) {
        SHuiMBhudTextTimeStr(@"请输入6-20位密码");
        return;
    }
    if (_txtPasswd.text != _txtSurePasswd.text) {
        SHuiMBhudTextTimeStr(@"两次输入密码不一致");
        return;
    }
    
    NSString *strUrl = [NSString stringWithFormat:user_forgetPwd_api];
    
    NSDictionary *parameter = @{@"phone":_txtMoblie.text,
                                @"password":_txtPasswd.text,
                                @"code":_txtCode.text};
    
    [PostNetWorking postNetWorkingStr:strUrl andparameters:parameter Success:^(id responseObject) {
        
        if ([responseObject[@"code"] intValue] == 200) {
            SHuiMBhudTextTimeStr(@"找回密码成功");
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                
                [self.navigationController popViewControllerAnimated:YES];
                
            });

        }else{
             SHuiMBhudTextTimeStr(responseObject[@"mark"]);
        }
        
        
    } failed:^(NSError *error) {
        SHuiMBhudTextTimeStr(@"网络错误");
    }];
    
}



@end
